package com.white.loggerserver.core

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.graphics.Color
import android.os.Handler
import android.os.IBinder
import android.os.Looper
import android.util.Log
import com.white.loggerserver.logpop.GlobleWindow
import com.white.loggerserver.logpop.LogBean
import com.white.loggerserver.room.MyRoomDb
import com.white.loggerserver.room.StyleConfig
import kotlin.properties.Delegates

/**
 * Created by ytf on 2020/07/23.
 * Description:
 */
class LogServerManager {

    companion object{
        val instance : LogServerManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { LogServerManager() }
    }

    private var mContext : Context by Delegates.notNull()

    fun init(ctx: Context){
        mContext = ctx
        intent = Intent(mContext, LogServerService::class.java)
        mContext.startService(intent)
        mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)
    }

    fun destroy(){
        communicateService?.run {
            mContext.unbindService(conn)
            mContext.stopService(intent)
        }
        closeLogWindow()
    }

    private var tempListener : UserListenCallback? = null
    fun registerCallback(callback: UserListenCallback){
        tempListener = callback
        communicateService?.apply {
            registerCallback(callback)
            tempListener = null
        }
    }

    fun unregisterCallback(callback: UserListenCallback){
        communicateService?.apply {
            unregisterCallback(callback)
        }
    }

    private var intent : Intent by Delegates.notNull()
    private var communicateService : LogServerService? = null
    private var conn = object : ServiceConnection {

        override fun onServiceDisconnected(name: ComponentName?) {
            communicateService = null
        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            communicateService = service?.let {
                (service as LogServerService.MyBinder).getService()
            }
            tempListener?.let {
                communicateService?.registerCallback(it)
            }
        }
    }

    var isShowing = false

    private val mHandler = Handler(Looper.getMainLooper())
    private var pop: GlobleWindow? = null
    fun showLogWindow()
    {
        if(isShowing) return

        pop?.dismissWindow()
        pop = GlobleWindow(mContext).also { it.setCallback {
            //todo do something after close
        } }
        pop?.show()
        isShowing = true
        registerCallback(windowCallback)
    }

    private var ibean : StyleConfig? = null
    private var dbean : StyleConfig? = null
    private var ebean : StyleConfig? = null
    private var needRefresh = true

    fun notifyStyleChanged(){
        needRefresh = true
    }

    private val windowCallback = object : UserListenCallback{
        override fun onClientOnline(ip: String) {

        }

        override fun onClientOffline(ip: String) {

        }

        override fun onReceiveMsg(ip: String, type: Int, timestamp: Long, msg: String) {
            var b : StyleConfig? = null
            val logBean = LogBean()
            if(needRefresh){
                ibean = null
                dbean = null
                ebean = null
                needRefresh = false
            }
            when(type){
                0 ->{
                    logBean.type = 0
                    if(ibean == null){
                        ibean = MyRoomDb.instance.styleConfigDao().getStyleByType("i")
                    }
                    b = ibean
                }
                1->{
                    logBean.type = 1
                    if(dbean == null){
                        dbean = MyRoomDb.instance.styleConfigDao().getStyleByType("d")
                    }
                    b = dbean
                }
                2->{
                    logBean.type = 2
                    if(ebean == null){
                        ebean = MyRoomDb.instance.styleConfigDao().getStyleByType("e")
                    }
                    b = ebean
                }
            }
            logBean.apply {
                time = timestamp
                content = msg
                fontSize = b?.fontSize?:12
                fontColor = b?.fontColor ?: Color.GREEN
                bgColor =b?.bgColor?:Color.TRANSPARENT
            }
            mHandler.post{
                pop?.postItem(logBean)
            }
        }
    }

    fun closeLogWindow()
    {
        isShowing = false
        pop?.dismissWindow();
        pop = null
    }
}